//
//  StatusOriginalView.swift
//  Weibo
//
//  Created by 冯童童 on 16/1/18.
//  Copyright © 2016年 冯童童. All rights reserved.
//

import UIKit
import SnapKit

class StatusOriginalView: UIView {
    
    var bottomConstraint : Constraint?
    var status : Status? {
        didSet {
            headImage.sd_setImageWithURL(status?.user?.headImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
            nameLabel.text = status?.user?.name
            mbrankImage.image = status?.user?.mbrank_image
            verifiedTypeImage.image = status?.user?.verified_type_image
//            timeLabel.text = status?.created_at
//            sourceLabel.text = status?.source
            contentLabel.text = status?.text
            pictureView.imageURLs = status?.imageURLs
            
            self.bottomConstraint?.uninstall()
            if let urls = status?.imageURLs {
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint =  make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint

                })
            }else {
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint =  make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                    
                })

            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){
        
        backgroundColor = UIColor(white: 1, alpha: 1)
        addSubview(headImage)
        addSubview(nameLabel)
        addSubview(mbrankImage)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedTypeImage)
        addSubview(contentLabel)
        addSubview(pictureView)
        addSubview(sepView)
        sepView.backgroundColor = UIColor.redColor()
        
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
        }
        headImage.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: StatusCellImageWidth, height: StatusCellImageWidth))
        
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImage.snp_top)
            make.left.equalTo(headImage.snp_right).offset(StatusCellMargin)
        }
        mbrankImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)

        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(headImage.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(headImage.snp_bottom)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(timeLabel.snp_bottom)

        }
        verifiedTypeImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImage.snp_right)
            make.centerY.equalTo(headImage.snp_bottom)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(headImage.snp_left)
            //make.right.equalTo(self.snp_right).offset(-StatusCellMargin)
            make.top.equalTo(headImage.snp_bottom).offset(StatusCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(headImage.snp_left)
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        self.snp_makeConstraints { (make) -> Void in
          self.bottomConstraint =  make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    }

    private lazy var sepView : UIView = UIView()
    private lazy var headImage : UIImageView = UIImageView()
    private lazy var nameLabel : UILabel = UILabel(title: "隔壁老王", fontSize: 14, color: themeColor)
    private lazy var mbrankImage : UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    private lazy var timeLabel : UILabel = UILabel(title: "22:22", fontSize: 10, color: UIColor.lightGrayColor())
    private lazy var sourceLabel : UILabel = UILabel(title: "22期的同学们", fontSize: 10, color: UIColor.lightGrayColor())
    private lazy var verifiedTypeImage : UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    private lazy var contentLabel : UILabel = UILabel(title: "任码虐我千百遍，我待代码如初恋", fontSize: 14, color: UIColor.darkGrayColor(), margin: StatusCellMargin)
    private lazy var pictureView : StatusCellPictureView = StatusCellPictureView()




    
}
